//
//  ConfirmCouponView.swift
//  koolcar
//
//  Created by xiantian on 2023/11/2.
//

import UIKit

class ConfirmCouponView: UIView {
    var emptyImgView: UIImageView!
    var titleLab: UILabel!
    var collectionView: UICollectionView!
    var dataArray: [CouponDataModel] = []
    override init(frame: CGRect) {
        super.init(frame: frame)
        emptyImgView = UIImageView()
        emptyImgView.image = UIImage.init(named: "icon_empty")
        emptyImgView.isHidden = true
        emptyImgView.contentMode = .scaleAspectFit
        self.addSubview(emptyImgView)
        
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize.init(width: kScreenWidth * 2/3, height: 98)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 0
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 98), collectionViewLayout: layout)
        self.collectionView.register(ConfirmCouponCollectCell.self, forCellWithReuseIdentifier: "ConfirmCouponCollectCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.white
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 10)
        collectionView.showsHorizontalScrollIndicator = false
        self.addSubview(self.collectionView)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        emptyImgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(40)
            make.width.equalTo(82)
            make.height.equalTo(82)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(98)
        }
    }
    
    ///赋值
    func setCouponData(coupons: [CouponDataModel]){
        if coupons.count == 0 {
            self.emptyImgView.isHidden = false
            self.collectionView.isHidden = true
        }else{
            self.emptyImgView.isHidden = true
            self.collectionView.isHidden = false
            self.dataArray = coupons
            self.collectionView.reloadData()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension ConfirmCouponView: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ConfirmCouponCollectCell", for: indexPath) as! ConfirmCouponCollectCell
        if self.dataArray.count > indexPath.row {
            let model = self.dataArray[indexPath.row]
            cell.setConfirmCouponCollectCell(data: model)
        }
        return cell
    }
}

